# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time
from utils.operators.rich_sql_sensor import RichSqlSensor
# from jms.dm.dm_whole_decide_duty_dt import jms_dm__dm_whole_decide_duty_dt

label = f"dm_whole_decide_duty_dt_{int(time())}"
timeout = timedelta(minutes=30).seconds
doris_jms_dm__dm_whole_decide_duty_dt = RichSqlSensor(
    task_id='doris_jms_dm__dm_whole_decide_duty_dt',
    pool='broker_load_pool',
    email=['suning@jtexpress.com','yl_bigdata@yl-scm.com'],
    conn_id='new_doris',
    pre_sql=f"""
    TRUNCATE TABLE jms_dm.dm_whole_decide_duty_dt PARTITION (
         p{{{{ execution_date | cst_ds_nodash }}}}
    );
    LOAD LABEL jms_dm.{label} (
        DATA INFILE(
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_dt/dt={{{{ execution_date | cst_ds }}}}/*")
        INTO TABLE dm_whole_decide_duty_dt
        FORMAT AS 'PARQUET'(
             waybill_no
            ,platform_effect
            ,ziyou_platform_effect
            ,if_platform_standard
            ,if_doudi_platform_standard
            ,cainiao_platform_name
            ,cainiao_platform_effect
            ,if_cainiao_platform_pres_standard
            ,first_duty_type
            ,if_intercept
            ,if_reback
            ,if_transfer
            ,start_city_code
            ,start_city_name
            ,sign_city_code
            ,sign_city_name
            ,taking_time
            ,deliver_time
            ,aging_sign_time
            ,actual_sign_time
            ,economic_circle_name
            ,scan_site_no
            ,scan_site_code
            ,scan_site_name
            ,scan_site_type
            ,scan_site_arrival_time
            ,scan_site_send_time
            ,scan_site_actual_arrival_time
            ,scan_site_actual_send_time
            ,scan_site_seal_time
            ,scan_site_actual_shipment_no
            ,scan_site_actual_shipment_name
            ,scan_site_arrival_bagg_code
            ,scan_site_arrival_bagg_name
            ,scan_site_send_bagg_code
            ,scan_site_send_bagg_name
            ,scan_site_send_bagg_addr_code
            ,scan_site_send_bagg_addr_name
            ,scan_site_arrival_bagg_addr_code
            ,scan_site_arrival_bagg_addr_name
            ,next_site_code
            ,next_site_name
            ,next_site_type
            ,actual_diff_time
            ,actual_node
            ,first_network_code
            ,first_network_name
            ,sign_network_code
            ,sign_network_name
            ,last_network_code
            ,last_network_name
            ,duty_type
            ,plan_last_taking_time
            ,plan_sign_time
            ,route_id
            ,if_taking_off
            ,total_days
            ,actual_effect
            ,scan_site_planned_departure_time
            ,scan_site_planned_arrival_time
            ,scan_site_if_stop
            ,cainiao_plan_sign_time        
            ,ziyou_plan_sign_time          
            ,scan_site_if_static           
            ,next_site_if_static           
        )
        columns from path as (dt)
        set(
             waybill_no                          = waybill_no
            ,platform_effect                     = platform_effect
            ,ziyou_platform_effect               = ziyou_platform_effect
            ,if_platform_standard                = if_platform_standard
            ,if_doudi_platform_standard          = if_doudi_platform_standard
            ,cainiao_platform_name               = cainiao_platform_name
            ,cainiao_platform_effect             = cainiao_platform_effect
            ,if_cainiao_platform_pres_standard   = if_cainiao_platform_pres_standard
            ,first_duty_type                     = first_duty_type
            ,if_intercept                        = if_intercept
            ,if_reback                           = if_reback
            ,if_transfer                         = if_transfer
            ,start_city_code                     = start_city_code
            ,start_city_name                     = start_city_name
            ,sign_city_code                      = sign_city_code
            ,sign_city_name                      = sign_city_name
            ,taking_time                         = taking_time
            ,deliver_time                        = deliver_time
            ,aging_sign_time                     = aging_sign_time
            ,actual_sign_time                    = actual_sign_time
            ,economic_circle_name                = economic_circle_name
            ,scan_site_no                        = scan_site_no
            ,scan_site_code                      = scan_site_code
            ,scan_site_name                      = scan_site_name
            ,scan_site_type                      = scan_site_type
            ,scan_site_arrival_time              = scan_site_arrival_time
            ,scan_site_send_time                 = scan_site_send_time
            ,scan_site_actual_arrival_time       = scan_site_actual_arrival_time
            ,scan_site_actual_send_time          = scan_site_actual_send_time
            ,scan_site_seal_time                 = scan_site_seal_time
            ,scan_site_actual_shipment_no        = scan_site_actual_shipment_no
            ,scan_site_actual_shipment_name      = scan_site_actual_shipment_name
            ,scan_site_arrival_bagg_code         = scan_site_arrival_bagg_code
            ,scan_site_arrival_bagg_name         = scan_site_arrival_bagg_name
            ,scan_site_send_bagg_code            = scan_site_send_bagg_code
            ,scan_site_send_bagg_name            = scan_site_send_bagg_name
            ,scan_site_send_bagg_addr_code       = scan_site_send_bagg_addr_code
            ,scan_site_send_bagg_addr_name       = scan_site_send_bagg_addr_name
            ,scan_site_arrival_bagg_addr_code    = scan_site_arrival_bagg_addr_code
            ,scan_site_arrival_bagg_addr_name    = scan_site_arrival_bagg_addr_name
            ,next_site_code                      = next_site_code
            ,next_site_name                      = next_site_name
            ,next_site_type                      = next_site_type
            ,actual_diff_time                    = actual_diff_time
            ,actual_node                         = actual_node
            ,duty_type                           = duty_type
            ,plan_last_taking_time               = plan_last_taking_time
            ,plan_sign_time                      = plan_sign_time
            ,route_id                            = route_id
            ,if_taking_off                       = if_taking_off
            ,total_days                          = total_days
            ,actual_effect                       = actual_effect
            ,scan_site_planned_departure_time    = scan_site_planned_departure_time
            ,scan_site_planned_arrival_time      = scan_site_planned_arrival_time
            ,scan_site_if_stop                   = scan_site_if_stop
            ,cainiao_plan_sign_time              = cainiao_plan_sign_time
            ,ziyou_plan_sign_time                = ziyou_plan_sign_time
            ,scan_site_if_static                 = scan_site_if_static
            ,next_site_if_static                 = next_site_if_static
            ,dt                                  = dt
        )
    )
    WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
    PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dm WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dm WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )

# doris_jms_dm__dm_whole_decide_duty_dt << jms_dm__dm_whole_decide_duty_dt
